<?php

namespace App\Http\Controllers\Home;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Session;
class InfoController extends Controller
{
    //
    public function update(Request $request)
    {
        $data = $request -> except('_token');
        $uid = session('master') -> id;
        // dd($data);  
        // dd($request); 
        if($request -> hasFile('picname'))
        {
            if($request -> file('picname'))
            {
                //处理头像
                // getClientOriginalExtension()    extension()
                $extension = $request -> file('picname') -> getClientOriginalExtension();
                // echo $extension;
                $filename = mt_rand(1000000,9999999).'.'.$extension;
                $dir = './uploads/avatar/';

                $request -> file('picname') -> move($dir,$filename);//storeAs
                $data['picname'] = '/uploads/avatar/'.$filename;
                //获取原来的图片地址
                $oldPicname = DB::table('users_info') -> where('uid',$uid) -> first() -> picname;
                if(!empty($oldPicname))
                {
                    if($oldPicname != '/uploads/avatar/default.jpg')
                    {   

                        // if($oldPicname == '/' || $oldPicname =='./')//自己加的判断防误删
                        //  {
                        //      return;
                        //  }
                        if(file_exists('.'.$oldPicname))
                        {
                            unlink('.'.$oldPicname);
                        }
                    }
                }

                $res1 = DB::table('users_info') ->  where('uid',$uid) -> update(['signture'=> $data['signture'],'picname' => $data['picname']]);
                // var_dump($userInfoData);
            }
        }else
        {
            $data['picname'] = '/uploads/avatar/default.jpg';
        }
        
        $res2 = DB::table('users') -> where('id',$uid) -> update(['phone' => $data['phone'],'username' => $data['username']]);
        if($res2)
        {
            return redirect('/home/info') -> with(['into' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
      
    }

    public function edit()
    {
        $uid = session('master') -> id;
        $data =DB::table('users')
        -> select('users.username','users.email','users.phone','users_info.picname','users.status','users.password','users_info.picname','users_info.signture')
        -> leftJoin('users_info','users.id','=','users_info.uid')
        -> where('users.id',$uid)
        ->first();
        // dd($data);
        return view('home.userinfo.user.edit',['info' => '修改信息','data' => $data]);
    }

    public function password()
    {
        return view('home.userinfo.user.reset',['info' => '修改密码']);
    }

    public function reset(Request $request)
    {
        $uid = session('master') -> id;
        $data =DB::table('users') -> where('id',$uid) -> first();
        $old = $data -> password;
        $old = Crypt::decryptString($old);
        // dd($old);
        $oldpassword = $request -> oldpassword;
        // $oldpassword = $oldpassword -> 
        // $oldpassword = $request ->all();
        // dd($oldpassword);

        if($old == $oldpassword)
        {
            // $data = $request -> except('_token');
        $data = $request -> password;
        $password = Crypt::encryptString($data);

        $res = DB::table('users') -> where('id',$uid) -> update(['password' => $password]);
        if($res)
        {
            return redirect('/home/info') -> with(['info' => '修改成功']);
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }else
    {
         return back() -> with(['info' => '原密码错误']);
    }
        
    }

    public function index()
    {
        $uid = session('master') -> id;
        $data =DB::table('users')
        -> select('users.username','users.email','users.phone','users.status','users.password','users_info.picname','users_info.signture')
        -> leftJoin('users_info','users.id','=','users_info.uid')
        -> where('users.id',$uid)
        ->first();
        // dd($data);
        return view('home.userinfo.user.index',['info' => '账户中心','data' => $data]);
    }
}
